package com.woniu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.entity.Floors;
import com.woniu.entity.HouseHoldinfos;
import com.woniu.entity.Households;
import com.woniu.mapper.FloorsMapper;
import com.woniu.mapper.HouseHoldinfosMapper;
import com.woniu.mapper.HouseholdsMapper;
import com.woniu.service.IFloorsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Gapper
 * @since 2021-03-29
 */
@Service
public class FloorsServiceImpl extends ServiceImpl<FloorsMapper, Floors> implements IFloorsService {

    @Autowired
    private FloorsMapper floorsMapper;

    @Autowired
    private HouseholdsMapper holdsMapper;

    @Autowired
    private HouseHoldinfosMapper houseHoldinfosMapper;

    /**
     查询全部,所有查询统一调用mapper中的find方法，自动生成动态sql
     */
    @Override
    public List<Floors> findAll() {
        QueryWrapper queryWrapper=new QueryWrapper();
        List list = floorsMapper.find(queryWrapper);
        return list;
    }

    /**
     根据id查一个
     */
    @Override
    public List<Floors> findById(String fid) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.in("Floor_ID",fid);
        List <Floors> list = floorsMapper.find(queryWrapper);
        return list;
    }

    /**
     删除一个
     删除楼层的同时删除，房屋信息和房屋详情
     */
    @Override
    @Transactional
    public int deleteById(String fid) {
        //hid
        QueryWrapper hquery=new QueryWrapper();
        hquery.in("Floor_ID",fid);
        List<Households> hlist = holdsMapper.selectList(hquery);
        //将hid存入集合
        Set hid=new HashSet();
        for (int i = 0; i < hlist.size(); i++){
            hid.add(hlist.get(i).getHouseholdId());
        }

        //hfid
        QueryWrapper hfquery=new QueryWrapper();
        hfquery.in("HouseHold_ID",hid);
        List<HouseHoldinfos> hflist = houseHoldinfosMapper.selectList(hfquery);
        //将hfid放入集合
        Set hfid=new HashSet();
        for(int i = 0; i < hflist.size(); i++){
            hfid.add(hflist.get(i).getHouseholdinfoId());
        }

        int rows=0;
        //删除房子详细信息
        if(hfid.size() > 0){
            rows =houseHoldinfosMapper.deleteBatchIds(hfid);
        }

        //删除房子
        if(hid.size() > 0){
            rows= holdsMapper.deleteBatchIds(hid);
        }

        //删除楼层信息
        rows=floorsMapper.deleteBatchIds(Arrays.asList(fid));

        return rows;
    }

    //根据单元查楼层
    @Override
    public List<Floors> findByUid(String uid) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("Unit_ID",uid);
        List<Floors> list = floorsMapper.selectList(queryWrapper);
        return list;
    }

    @Override
    public List<Floors> find() {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.groupBy("Floor_Name");
        List<Floors> list = floorsMapper.find(queryWrapper);
        return list;
    }
}
